import datetime

from ninja import Router

from apps.commom.water_droplets import WaterDropletsTaskSerializer
from apps.schemas import R
from models.models import WaterDropletsTask, WaterDropletsRecord
from utils.auth import AppletAuthBearer
from utils.decorators import handle_exception

applets_water_droplets_task_router = Router(tags=["用户获取水滴任务"], auth=AppletAuthBearer())


@applets_water_droplets_task_router.get("/water_droplets_tasks", summary="获取水滴任务列表")
@handle_exception("获取水滴任务列表失败")
def get_water_droplets_tasks(request):
    tasks = WaterDropletsTask.objects.all()
    return R.ok(WaterDropletsTaskSerializer(tasks).data)


@applets_water_droplets_task_router.get("/water_droplets_task/daily", summary="获取水滴每日任务",
                                        auth=AppletAuthBearer())
@handle_exception("获取水滴每日任务失败")
def get_daily_water_task(request):
    """
    获取水滴每日任务
    """
    today = datetime.datetime.now()
    date_zero = datetime.datetime(today.year, today.month, today.day, 0, 0, 0)
    record = WaterDropletsRecord.objects.filter(op_time__gte=date_zero)
    result = []
    for item in [2, 3, 4]:
        task = WaterDropletsTask.objects.get(pk=item)
        if record.filter(task=task).exists():
            result.append({"task_name": task.task_name, "is_completed": True})
        else:
            result.append({"task_name": task.task_name, "is_completed": False})
    return R.ok(result)

